Arexx Tutorial

By Josef Faulkner (panther@gate.net)


1.0 - Introduction

I am assuming you know what Arexx is and the history behind it. If you don't know, this information is readily available in the first chapter of the Arexx User's Guide that comes with Workbench. This book is also a very handy reference, so dont lose it! :)

If you are familiar with any programming language from Basic to C, you will have no problem picking up Arexx. Arexx is one of the easiest languages to learn and to work with. When writing arexx programs, you do not need to deal with silly line numbers (as in basic), or worry that the computer will crash if it doesnt correctly open a library (as in C). Arexx is very difficult to crash on its own, so feel free to explore it.

Arexx is one of the things that makes the Amiga one of the best Personal Computers anyone can own. The Amiga was designed to multitask in such a way that even operating systems built 10 years later still have yet to fully close the gap on the efficiency and integration of the Amiga multitasking environment. Arexx takes advantage of the Amiga's ability to multitask by allowing us to greatly customize our favorite applications. However, it is not necessary to write arexx scripts for programs, but to write an entire program in Arexx, independent of any applications.

For example:
  /* calc.rexx - A simple command line calculator */
  parse arg args
  interpret 'say 'args
  exit
  /* End */
    

Don't worry about what this says yet, I plan to explain all of this later. To use this script, cut it out, and save it as rexx:calc.rexx. Be sure to include the comment at the top. To use it, type rx calc 2+2 in the CLI. This will also be a helpful debugging tool later on.

1.1 - Making sure everything is in place

Arexx needs a few things in place in order for it to work. Workbench handles most of this, but it seems to have left out some important details.

  1. Getting the Rexx: directory straight.
  2. Making sure RexxMast is run.
  3. Making sure RexxC is in the path
  4. File check

1.2 - Usage

There are two ways you can run an Arexx command. One is to use the RX command located in the SYS:RexxC directory, the other is to use the script file flag to tell AmigaDOS that the script is a command.
Examples:
1> rx calc.rexx 2+2
4
1>

1> protect rexx:calc.rexx +S
1> calc.rexx 2+2
4
1>

The second method requires that REXX: is added to your path.

1.3 - Comments

All arexx script files must begin with a comment. This comment tells the arexx interpreter, as well as AmigaDOS with +S files, that this is an Arexx script.
Example:
/* This is a comment */

It doesnt matter what you put in the comment, however it is standard practice to at least include a short description of what the program does. However, try to minimize long comments at the beginning of frequently used scripts, as it might slow down the execution of the script. It is best to put long descriptions and such at the end of the arexx script, so that it isnt scanned by the interpreter.

Unlike C, comments may be nested in Arexx, although I dont see much use for it :)

1.4 - Variables

All variables in Arexx are stored as strings. However, when numerical operations are done, Arexx will automatically treat a string holding numbers to a number, do the operation, then store the result back in a string. This has great advantages, since you wont have to do any casting, and you can mix numbers with strings very easily.

For example, in lower level languages, if you wanted a user to be able to enter either a string or a number into a variable, you have to read their input into a string, then if that string is a number, convert it to a number variable. In arexx, you simply take the input, and then you can immediately use it however you wish.

Example:
  /* ( variable.rexx ) - Shows how variables can be used */
  variable='TEXT'
  say variable
  variable=variable' '15   /* Puts the string "15" as another word */
  say variable
  say word(variable,2)*2   /* word() is a function that returns a specified */
                           /* word from a string.  In this case we are      */
                           /* looking at the second word, which is 15.      */
                           /* This will say 30 as the answer since we are   */
                           /* multiplying the second word of variable by 2  */
  exit
    

Concatenation is joining two strings together. When we put the 15 in variable, we told arexx to leave a space so that 15 would be the second word. If you do not want a space, you can join two strings using the || operator (two pipes).

Appendix A

Arithmetic Operators

  Char  Operation          Pri
  ----- ------------------ ---
   **   Exponentiation      7
   /    Division            6
   %    Integer Division    6
   //   Modulo (Remainder)  6
   +    Addition            5
   -    Subtraction         5

Comparison Operators

  Char  Operation
  ----- ------------------------
   ==   Exact equality
   ~==  Exact Inequality
   =    Equality
   ~=   Inequality
   >    Greater Than
   >=   Greater Than or Equal To -.
   ~<   Not Less Than            -`-- Same thing
   <    Less Than
   <=   Less Than or Equal To    -.
   ~>   Not Greater Than         -`-- Same thing

Logical Operators

  Char Type Pri
  ---- ---- ---
   ~   NOT   8
   &   AND   2
   |   OR    1
   ^   XOR   1

Other Characters

  Char Description
   :   Procedure/Function Label
   ()  Group operators and operands to override priorites
   ;   Statement Terminator
   ,   Marks a continuation of a line (to a new line)

Written exclusively for Amiga Report Technical Journal